﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WrapperCore;

namespace Online_Math
{
    public partial class Solve : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void solveButton_Click(object sender, EventArgs e)
        {

            // We fill the constants list with whatever is in the select element
            List<string> constants = new List<string>();
            foreach (ListItem item in constantsList.Items)
            {
                constants.Add(item.ToString());
            }

            // We get the variables
            string dependentVar = dependentVariableText.Text;
            string independentVar = independentVariableText.Text;

            // We convert and search the converted query in wolfram alpha style
            MathOnlineConverter converter = new MathOnlineConverter(constants, dependentVar, independentVar);
            string query = converter.ConvertQuery(inputQueryText.Text);
            string answer = converter.Search(query); // We get the html answer converted back to input style.

            ClientScript.RegisterStartupScript(this.GetType(), "hash", "goToResultsAnchor();", true); // We go to our answer.

            solutionText.Text = answer;
        }

        protected void addConstantButton_Click(object sender, EventArgs e)
        {
            constantsList.Items.Add(addConstantText.Text);
            addConstantText.Text = "";
        }

        protected void deleteSelectedButton_Click(object sender, EventArgs e)
        {
            int selected = constantsList.SelectedIndex;
            if (selected != -1)
            {
                constantsList.Items.RemoveAt(selected);
            }
        }

        protected void clearAllButton_Click(object sender, EventArgs e)
        {
            constantsList.Items.Clear();
        }

        protected void findConstantsButton_Click(object sender, EventArgs e)
        {
            List<string> constants = MathOnlineConverter.FindConstants(inputQueryText.Text);
            constantsList.Items.Clear();
            foreach (string constant in constants)
            {
                constantsList.Items.Add(constant);
            }
        }

        protected void showExampleButton_Click(object sender, EventArgs e)
        {
            if (exampleImage.Visible)
            {
                exampleImage.Visible = false;
                showExampleButton.Text = "↓ Show Example";
            }
            else
            {
                exampleImage.Visible = true;
                showExampleButton.Text = "↑ Hide Example";
            }
        }

    }
}
